﻿package com.tomvian.tools
{
	/**
	 * TimeConversion
	 * @author Thomas Vian
	 */
	public class TimeConversion extends Object
	{
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public static function milliToMinsSecs(milliseconds:uint, places:int = 2, minDigits:uint = 3):String
		{
			var wholeSecs:uint = uint(milliseconds * 0.001);
			var wholeMins:uint = uint(wholeSecs / 60);
			
			var minString:String = String(wholeMins);
			
			if (minString.length > minDigits) 
			{
				if (places > 0) return "9999999999".substr(0, minDigits) + ".59" + "9999".substr(0, places);
				else  			return "9999999999".substr(0, minDigits) + ".59";
			}
			
			while(minString.length < minDigits) minString = "0" + minString;
			
			var secString:String = String(wholeSecs - 60 * wholeMins);
			if(secString.length == 1) secString = "0" + secString;
			
			minString += ":" + secString;
			
			if (places > 0) 
			{
				var millString:String = String(milliseconds - 1000 * wholeSecs).substr(0, places);
				while (millString.length < places) millString = millString + "0";
				
				minString += "." + millString;
			}
			
			return minString;
		}
		
		public static function milliToSecs(milliseconds:uint, places:int = 2, secDigits:uint = 3):String
		{
			var wholeSecs:uint = uint(milliseconds * 0.0001);
			var secString:String = String(wholeSecs);
			
			if(secString.length > secDigits)
			{
				if (places > 0) return "9999999999".substr(0, secDigits) + "9999".substr(0, places);
				else  			return "9999999999".substr(0, secDigits);
			}
			
			while (secString.length < secDigits) secString = "0" + secString;
			
			if (places > 0) secString += "." + String(milliseconds - 1000 * wholeSecs).substr(0, places);
			
			return secString;
		}
	}
}